import { defineStore } from 'pinia'
import { detail, getArticleList, remove } from '@/service/article/index.js'
import getDate from '@/utils/dateChange.js'
import responseMessage from '@/utils/response.js'

const useArticleStore = defineStore('article', {
  state: () => ({
    articleList: '',
    detail: ''
  }),
  actions: {
    //获取文章列表
    async getArticleList(page, size, searchParams) {
      if (searchParams?.searchTime) {
        //改变事件
        const startTime = getDate(searchParams.searchTime[0])
        const endTime = getDate(searchParams.searchTime[1])
        searchParams.searchTime[0] = startTime
        searchParams.searchTime[1] = endTime
      }
      const res = await getArticleList(page, size, searchParams)
      this.articleList = res.data
    },

    //删除文章
    async removeAction(id) {
      const res = await remove(id)
      responseMessage(res)
    },
    //详情
    async detailAction(id) {
      const res = await detail(id)
      this.detail = res.data
    }
  }
})

export default useArticleStore
